package com.huang.auth.converter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.auth.constants.MenuWrapperConstant;
import com.huang.auth.dao.AuthMenuDao;
import com.huang.auth.entity.po.AuthMenuPO;
import com.huang.auth.entity.vo.AuthMenuVO;
import com.huang.base.core.BaseConvertor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @Author HuangShen
 * @Date 2022/1/28 15:03
 * @Describe 菜单转换器
 */
@Component
public class AuthMenuVOConverter extends BaseConvertor<AuthMenuPO, AuthMenuVO> {
    @Resource
    private AuthMenuDao authMenuDao;

    @Override
    public AuthMenuVO doConvert(AuthMenuPO m) {
        AuthMenuVO authMenuVO = new AuthMenuVO();
        BeanUtils.copyProperties(m, authMenuVO);
        authMenuVO.setId(m.getId()+"$"+new Random().nextInt(100));
        QueryWrapper<AuthMenuPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(MenuWrapperConstant.ID);
        queryWrapper.eq(MenuWrapperConstant.PARENT_ID,m.getId());
        Long aLong = this.authMenuDao.selectCount(queryWrapper);
        authMenuVO.setHasChildren(Boolean.FALSE);
        if (aLong > 0) {
            authMenuVO.setHasChildren(Boolean.TRUE);
        }
        return authMenuVO;
    }
}
